<?php
require_once("nusoap/nusoap.php");
require_once("utils.php");

class ContactSoap{
    	var $proxy;
        var $sess;
	var $moduleName;
	var $defaultOrderBy;
	var $columnsList;
	var $requestedColulmnsList;
	var $rowCount;
	
        function ContactSoap(){
		
			$this->proxy = $_SESSION["sugar_soap_proxy"];
			$this->sess = $_SESSION["sugar_session_id"];
			$this->rowCount = 0;
			$this->moduleName = "Contacts";
			$this->columsList = array();

                        $this->columnsList = getColumnsList();

                        if (count($this->columnsList) == 0) {
                                $this->columnsList = array(
                                        'photo_c',
                                        'salutation',
                        		'first_name',
                        		'last_name',
                                        'account_id',
                                        'lead_source',
                                        'campaign_id',
                                        'title',
                                        'department',
                                        'assigned_user_id',
                        		'email1',
                        		'phone_work',
                                        'phone_mobile',
                                        'phone_home',
                                        'phone_other',
                                        'phone_fax',
                                        'do_not_call',
                                        'primary_address_street',
                                        'primary_address_city',
                                        'primary_address_state',
                                        'primary_address_postalcode',
                                        'primary_address_country',
                                        'description',
                                        'created_by',
                                        );
                        }

			$this->requestedColumnsList = $this->columnsList;

                        $this->columnsList[] = 'id';
                        $this->columnsList[] = 'date_modified';
                        $this->columnsList[] = 'deleted';
			
        }
				
				
		function getContacts($query, $startIndex, $numberOfRows, $orderBy=' contacts.date_modified desc', $includeDeleted){
		
			$recordCount = 0;
	
    		$result = $this->proxy->get_entry_list(
        		$this->sess,
        		$this->moduleName,
        		$query,
        		$orderBy,
        		$startIndex,
        		array(
            		'id',
					'photo_c',
					'salutation',
            		'first_name',
            		'last_name',
					'account_id',
					'lead_source',
					'campaign_id',
					'title',
					'department',
					'assigned_user_id',
            		'email1',
            		'phone_work',
					'phone_mobile',
					'phone_home',
					'phone_other',
					'phone_fax',
					'do_not_call',
					'primary_address_street',
					'primary_address_city',
					'primary_address_state',
					'primary_address_postalcode',
					'primary_address_country',
					'description',
					'date_modified',
					'created_by',
					'deleted'
        		),
        		$numberOfRows,
        		false
    		);
			
			$returnList = array();
			
			if ($result == "" || count($result) == 0)
				return $returnList;
			
			foreach ($result['entry_list'] as $record) {
					$returnList[] = $record;
			}
			
			$this->rowCount = count($returnList);

			if ($includeDeleted && $this->rowCount < $numberOfRows) {
				
			
				if ($deleteNumberOfRows != -1) {
					$deleteResult = $this->getDeletedContacts($query, 0, $numberOfRows - $this->rowCount, $orderBy);
					
					if ($deleteResult != "" && count($deleteResult) > 0) {
					
						foreach ($deleteResult['entry_list'] as $record) {
							$returnList[] = $record;
						}
					}
					
				}
			} 
			
			$this->rowCount = count($returnList);

			
			if ($numberOfRows > 0)
				$returnList = array_slice($returnList, 0, $numberOfRows);
    		return $returnList;
		}
		
		function getRowCount() {
		
			return $this->rowCount;
		}
		
		
		function getTotalRows($query, $includeDeleted) {
		
			$result = $this->proxy->get_entries_count(
        		$this->sess,
        		$this->moduleName,
        		$query,
				$includeDeleted);
				
			return $result['result_count'];
	
		}
		
		function getDeletedContacts($query, $startIndex, $numberOfRows, $orderBy=' contacts.date_modified desc'){
    		$result = $this->proxy->get_entry_list(
        		$this->sess,
        		$this->moduleName,
        		$query,
        		$orderBy,
        		$startIndex,
        		array(
            		'id',
					'photo_c',
					'salutation',
            		'first_name',
            		'last_name',
					'account_id',
					'lead_source',
					'campaign_id',
					'title',
					'department',
					'assigned_user_id',
            		'email1',
            		'phone_work',
					'phone_mobile',
					'phone_home',
					'phone_other',
					'phone_fax',
					'do_not_call',
					'primary_address_street',
					'primary_address_city',
					'primary_address_state',
					'primary_address_postalcode',
					'primary_address_country',
					'description',
					'date_modified',
					'created_by',
					'deleted'
        		),
        		$numberOfRows,
        		true
    		);
			
    		return $result;
		}
		
		
		function saveContact($id, $photo_c, $salutation, $first_name, $last_name,
 						     $account_id, $lead_source, $campaign_id,
							 $title, $department, $assigned_user_id,
							 $email1, $phone_work, $phone_mobile, $phone_home, $phone_other,
							 $phone_fax, $do_no_call, $primary_address_street,
							 $primary_address_city, $primary_address_state,
							 $primary_address_postalcode, 
							 $primary_address_country, $description,
							 $date_modified) {
		
		
			if ($id) {
				$response = $this->proxy->set_entry($this->sess, 'Contacts', 
								array(array("name" => 'id',"value" => $id),
									  array("name" => 'photo_c',"value" => $photo_c),
									  array("name" => 'salutation',"value" => $salutation),
								      array("name" => 'first_name',"value" => $first_name),
            					      array("name" => 'last_name',"value" => $last_name),
									  array("name" => 'account_id',"value" => $account_id),
									  array("name" => 'lead_source',"value" => $lead_source),
									  array("name" => 'campaign_id',"value" => $campaign_id),
									  array("name" => 'title',"value" => $title),
									  array("name" => 'department',"value" => $department),
									  array("name" => 'assigned_user_id',"value" => $assigned_user_id),
									  array("name" => 'email1',"value" => $email1),
									  array("name" => 'phone_work',"value" => $phone_work),
									  array("name" => 'phone_mobile',"value" => $phone_mobile),
									  array("name" => 'phone_home',"value" => $phone_home),
									  array("name" => 'phone_other',"value" => $phone_other),
									  array("name" => 'phone_fax',"value" => $phone_fax),
									  array("name" => 'do_not_call',"value" => $do_not_call),
									  array("name" => 'primary_address_street',"value" => $primary_address_street),
									  array("name" => 'primary_address_city',"value" => $primary_address_city),
									  array("name" => 'primary_address_state',"value" => $primary_address_state),
									  array("name" => 'primary_address_postalcode',"value" => $primary_address_postalcode),
									  array("name" => 'primary_address_country',"value" => $primary_address_country),
            						  array("name" => 'description',"value" => $description),
            					      array("name" => 'date_modified',"value" => $date_modified)
            						 )
            				);
			} else {
				$response = $this->proxy->set_entry($this->sess, 'Contacts', 
								array(array("name" => 'photo_c',"value" => $photo_c),
								      array("name" => 'salutation',"value" => $salutation),
								      array("name" => 'first_name',"value" => $first_name),
            					      array("name" => 'last_name',"value" => $last_name),
									  array("name" => 'account_id',"value" => $account_id),
									  array("name" => 'lead_source',"value" => $lead_source),
									  array("name" => 'campaign_id',"value" => $campaign_id),
									  array("name" => 'title',"value" => $title),
									  array("name" => 'department',"value" => $department),
									  array("name" => 'assigned_user_id',"value" => $assigned_user_id),
									  array("name" => 'email1',"value" => $email1),
									  array("name" => 'phone_work',"value" => $phone_work),
									  array("name" => 'phone_mobile',"value" => $phone_mobile),
									  array("name" => 'phone_home',"value" => $phone_home),
									  array("name" => 'phone_other',"value" => $phone_other),
									  array("name" => 'phone_fax',"value" => $phone_fax),
									  array("name" => 'do_not_call',"value" => $do_not_call),
									  array("name" => 'primary_address_street',"value" => $primary_address_street),
									  array("name" => 'primary_address_city',"value" => $primary_address_city),
									  array("name" => 'primary_address_state',"value" => $primary_address_state),
									  array("name" => 'primary_address_postalcode',"value" => $primary_address_postalcode),
									  array("name" => 'primary_address_country',"value" => $primary_address_country),
            						  array("name" => 'description',"value" => $description),
            					      array("name" => 'date_modified',"value" => $date_modified)
									  )
            			);
            }				 
			return $response;
		}
		
		function deleteContact($id) {
		
			$response = $this->proxy->set_entry($this->sess, 'Contacts', 
								array(array("name" => 'id',"value" => $id),
									  array("name" => 'deleted',"value" => 1),
            						 )
            				);
		
			return $response;
		
		}
		
		function nameValuePairToSimpleArray($array){
    		$my_array=array();
    		while(list($name,$value)=each($array)){
        		$my_array[$value['name']]=$value['value'];
    		}
    		return $my_array;
		} 
		
		
	}
	
$contactSoap = new ContactSoap();

$salutationsArray = array("" => "", 
 						  "Mr." => "Mr.", 
						  "Ms." => "Ms.", 
						  "Mrs." => "Mrs.", 
						  "Dr." => "Dr.", 
						  "Prof." => "Prof.");
						  
$leadsArray = array("" => "",
				    "Cold Call" => "Cold Call",
					"Existing Customer" => "Existing Customer",
					"Self Generated" => "Self Generated",
					"Employee" => "Employee",
					"Partner" => "Partner",
					"Public Relations" => "Public Relations",
					"Direct Mail" => "Direct Mail",
					"Conference" => "Conference",
					"Trade Show" => "Trade Show",
					"Web Site" => "Web Site",
					"Word of mouth" => "Word of mouth",
					"Email" => "Email",
					"Campaign" => "Campaign",
					"Other" => "Other");
?>
